<?php
// Module Body

function ssl_add_menu(&$data) {
	global $menu;

	if ($data['domaindata']['serverside']['ssl'] == 'on') {
		$menu['domain'][] = 'ssl';
	}
}

function ssl_add_lang(&$data) {
	global $T;
	switch($data['lang']) {
		default:
			/*if ($data['domaindata']['serverside']['ssl'] == 'on') {
				$T['domain']['ssl'] = 'SSL Certificates';
			}*/
			$T['menu']['ssl'] = 'SSL Certificates';

			$T['SSL not enabled']           = 'SSL is not enabled on this site';
			$T['Generate CSR']              = 'Generate CSR';
			$T['updatecertinfo']              = 'Update Cert Info';
			$T['Generate TEMP CERT']        = 'Generate Temporary Certificate';
			$T['Update Certificate']      = 'Update Certificate';
			$T['gencsrmsg1']      = 'Generating CSR for';
			$T['certcountryname']   = 'Country Name (2 character code)';
			$T['certstate']   = 'State or Province';
			$T['certlocality']   = 'Locality/City';
			$T['certorgname']   = 'Organization Name';
			$T['certorgunit']   = 'Organizational Unit Name';
			$T['certinfoupdated'] = 'Updated Cert Info';
			$T['PHP was not compiled with OpenSSL <I>(--with-openssl)</I>.'] = 'PHP was not compiled with OpenSSL <I>(--with-openssl)</I>.';
			$T['Private Key:'] = 'Private Key:';
			$T['Certificate Signing Request (CSR):'] = 'Certificate Signing Request (CSR):';
			$T['Certificate:'] = 'Certificate:';
			$T['ss']['ssl']	= 'SSL';
			break;
	}
}

function ssl_add_cfg(&$cfg) {
	$cfg['OPEN_SSL_CONF_PATH'] = '/usr/share/ssl/openssl.cnf'; //point to your config file
	$cfg['OPEN_SSL_CERT_DAYS_VALID'] = 365;//1 year
	$cfg['OPEN_SSL_IS_FILE'] = 1;
	$cfg['sslang'] .= ',ssl';
}

function server_ssl_generate(&$data) {
	global $cfg;

	if ($data['type'] == 'domain') {
		// Find SSL directory path
		$siteid = $data['id'];
		$ssl = $cfg['webdir']."/".$data['id']."/".$data['host']."/ssl/";

		if (is_dir($ssl)) {
			$servercrt = $ssl."server.crt";
			$serverkey = $ssl."server.key";

			if ($data['domaindata']['serverside']['ssl'] == 'on') {
				ssl_extract_privkey_file($siteid, $serverkey);
				ssl_extract_cert_file($siteid, $servercrt);
			}
		}
	}
}

function ssl_domain_cert(&$data) {
	global $T, $cfg;
	$cp = $data['_cp'];
	$url = $data['_url'];
	$action = $data['_action'];
	$confirm = $data['_confirm'];
	$userdata = $data['_userdata'];
	$personaldata = $data['_personaldata'];
	$domaindata = $data['_domaindata'];
	$resellerdata = $data['_resellerdata'];
	$cp = $data['_cp'];
	$url = $data['_url'];
	$action = $data['_action'];
	$confirm = $data['_confirm'];
	$current_url = $data['_current_url'];

	switch($data['func']) {
		case "generatecsr":
			$cn=$domaindata['host'] . '.' . $domaindata['domain'];
			msg_display($T['gencsrmsg1'] . ': ' . $cn);
			ssl_generate_csr($domaindata['id'],$cn);
			break;
		case "updatecertinfo":
			msg_tag_display('certinfoupdated');
			$cn=$domaindata['host'] . '.' . $domaindata['domain'];
			ssl_save_identity($domaindata['id'],$data['sslcountryname'],$data['sslstate'],$data['ssllocality'],
			$data['sslorgname'],$data['sslorgunit'],$cn);
			break;
		case "updatecert":
			msg_tag_display('certupdated');
			$cn=$domaindata['host'] . '.' . $domaindata['domain'];
			ssl_set_cert($domaindata['id'],$cn,$data['cert']);
			$SQL = 'UPDATE domains SET action="update" WHERE id = '.$domaindata['id'];
			mysql_query($SQL);
			commit('scan');
			break;
		case "gentempcert":
			$cn=$domaindata['host'] . '.' . $domaindata['domain'];
			ssl_generate_temp_cert($domaindata['id'],$cn);
			$SQL = 'UPDATE domains SET action="update" WHERE id = '.$domaindata['id'];
			mysql_query($SQL);
			commit('scan');
			break;
		default:
			break;
	}

// If we're back from a reload (i.e confirm is set), display message.
if ($confirm)
	msg_tag_display('domain updated');

	$certdata=fetch_certinfo($domaindata['id']);

if (!function_exists('openssl_verify')) {
	echo $T['PHP was not compiled with OpenSSL <I>(--with-openssl)</I>.'];
} elseif ($domaindata['serverside']['ssl'] == 'on') {
?>

<div align="center">
<form action="<?=$current_url;?>" method="post" name="webcp" onSubmit="submitonce(this);">
<table border="0" cellspacing="0" cellpadding="0"><tr><td>
<table border="0" cellspacing="0" cellpadding="0" class="tblbg">
	<tr>
		<td>
		<table border="0" cellspacing="1">
			<tr>
				<td colspan="2" class="tbltop">&nbsp;<?=$T['menu'][$url];?>&nbsp;</td>
			</tr>
			<tr>
				<td class="row1"><b>&nbsp;<?=$T['IP Address'];?>&nbsp;</b></td>
				<td class="row2">&nbsp;<?
					echo $domaindata['ip']; ?></select>&nbsp;</td>
			</tr>
			<tr>
				<td class="row1"><b>&nbsp;<?=$T['Hostname'];?>&nbsp;</b></td>
				<td class="row2">&nbsp;<? echo $domaindata['host'] . '.' . $domaindata['domain'] ;?>&nbsp;</td>
			</tr>
			<tr>
				<td class="row1"><b>&nbsp;<?=$T['certcountryname'];?>&nbsp;</b></td>
				<td class="row2">&nbsp;<? echo '<input type="text" name="data[sslcountryname]" size="2" maxlength="2" value="'.$certdata['countryname'].'">'; ?>&nbsp;</td>
			</tr>

			<tr>
				<td class="row1"><b>&nbsp;<?=$T['certstate'];?>&nbsp;</b></td>
				<td class="row2">&nbsp;<? echo '<input type="text" name="data[sslstate]" size="20" maxlength="64" value="'.$certdata['state'].'">'; ?>&nbsp;</td>
			</tr>

			<tr>
				<td class="row1"><b>&nbsp;<?=$T['certlocality'];?>&nbsp;</b></td>
				<td class="row2">&nbsp;<? echo '<input type="text" name="data[ssllocality]" size="20" maxlength="64" value="'.$certdata['locality'].'">'; ?>&nbsp;</td>
			</tr>

			<tr>
				<td class="row1"><b>&nbsp;<?=$T['certorgname'];?>&nbsp;</b></td>
				<td class="row2">&nbsp;<? echo '<input type="text" name="data[sslorgname]" size="30" maxlength="64" value="'.$certdata['orgname'].'">'; ?>&nbsp;</td>
			</tr>

			<tr>
				<td class="row1"><b>&nbsp;<?=$T['certorgunit'];?>&nbsp;</b></td>
				<td class="row2">&nbsp;<? echo '<input type="text" name="data[sslorgunit]" size="30" maxlength="64" value="'.$certdata['orgunit'].'">'; ?>&nbsp;</td>
			</tr>

</table>
<br><div align="right"></div>
</td></tr></table></table>
<BR>
<input type="submit" value="<?=$T['updatecertinfo'];?>">
<input type="hidden" name="data[func]" value="updatecertinfo">
</form>

<table>
<tr>
<td>
<?
if ($certdata['privatekey'] != '') {
	echo "<BR>".$T['Private Key:']."<BR><TEXTAREA ROWS=16 COLS=65 READONLY>".HTMLENTITIES($certdata['privatekey'])."</TEXTAREA>";
}
?>
</td>
</tr>
<tr>
<td>
<?
if ($certdata['csr'] != '') {
	echo "<br>".$T['Certificate Signing Request (CSR):'].":<br><TEXTAREA ROWS=16 COLS=65 READONLY>".HTMLENTITIES($certdata['csr'])."</TEXTAREA><br><br>";
}
?>
</td>
</tr>
<tr>
<td>
<form action="<?=$current_url;?>" method="post" name="webcp2" onSubmit="submitonce(this);">
<input type="hidden" name="data[func]" value="generatecsr">
<input type="submit" value="<?=$T['Generate CSR'];?>">
</form>
</td>
</tr>
<tr>
<td>
<form action="<?=$current_url;?>" method="post" name="webcp4" onSubmit="submitonce(this);">
<br><br><?=$T['Certificate:'];?><br><TEXTAREA NAME=data[cert] ROWS=18 COLS=65>
<?
if ($certdata['cert'] != '') {
	echo HTMLENTITIES($certdata['cert']);
}
?>
</TEXTAREA><br><br>
</td>
</tr>
<tr>
<td>
<input type="submit" value="<?=$T['Update Certificate'];?>">
<input type="hidden" name="data[func]" value="updatecert">
</form>
</td>
</tr>
<tr>
<td><br>
<form action="<?=$current_url;?>" method="post" name="webcp3" onSubmit="submitonce(this);">
<input type="hidden" name="data[func]" value="gentempcert">
<input type="submit" value="<?=$T['Generate TEMP CERT'];?>">
</form>
</td>
</tr>
</table>
</tr>
</table>
<?
} else {
	echo $T['SSL not enabled'] . "\n";
}
?>
</div>
<?
}

include_once($cfg['basedir'].'/modules/ssl.mod.inc.phps');
?>
